// pages/login/login.js
var app = getApp()
var WXBizDataCrypt = require('../../utils/RdWXBizDataCrypt.js');
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber: ''//联系电话
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onShow() {

  },

  onClickClose() {
    if (app.globalData.scanId != 0 || app.globalData.webUrl != '') {
      app.globalData.scanId = 0;
      app.globalData.webUrl = '';
      wx.switchTab({
        url: '/pages/index/index'
      });
    } else {
      wx.navigateBack({
        delta: getCurrentPages().length
      })
    }
  },


  //获取手机号
  getPhoneNumber(e) {
    if (e.detail.iv) {
      Toast.loading({ message: '授权中...', forbidClick: true });
      app.getOpenId().then((res) => {
        let time1 = setTimeout(() => {
          console.log(res.data.session_key);
          let pc = new WXBizDataCrypt('wx73ed89b05fa50dac', res.data.session_key);
          let data = pc.decryptData(e.detail.encryptedData, e.detail.iv);
          let login = wx.getStorageSync('login');
          login.phoneNumber = data.phoneNumber;
          wx.setStorageSync('login', login);
          clearTimeout(time1);
          let time = setTimeout(() => {
            Toast.success({ message: '授权成功，请完善用户信息', forbidClick: true });
            wx.redirectTo({
              url: '/pages/loginInfo/loginInfo'
            })
            clearTimeout(time);
          }, 200)
        }, 500);
      })
    }
  }
})